//
//  ViewController.m
//  FileOperationDemo
//
//  Created by Realank on 16/5/21.
//  Copyright © 2016年 iMooc. All rights reserved.
//

#import "ViewController.h"
#import "MCFileManager.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)printDirectory:(id)sender {
    
    //打印主要路径
    NSString* home = [MCFileManager homeDirectory];
    NSLog(@"沙盒根目录：%@",home);
    
    NSString* library = [MCFileManager libraryDirectory];
    NSLog(@"Library目录：%@",library);
    
    NSString* cache = [MCFileManager cacheDirectory];
    NSLog(@"Cache目录：%@",cache);
    
    NSString* tmp = [MCFileManager tmpDirectory];
    NSLog(@"tmp目录：%@",tmp);
}
- (IBAction)editPath:(id)sender {
    //路径字符串的拼接和分离
    
    //Cache目录
    NSString* cache = [MCFileManager cacheDirectory];
    NSLog(@"Cache目录：%@",cache);
    
    //追加路径
    NSString* newDir = [cache stringByAppendingPathComponent:@"newDir"];
    NSLog(@"新的路径：%@",newDir);
    
    //获取某一个路径的父路径
    NSString* fatherDir = [newDir stringByDeletingLastPathComponent];
    NSLog(@"新路径的父路径：%@",fatherDir);
    
    //创建新的文件路径
    NSString* newFile = [cache stringByAppendingPathComponent:@"newFile"];
    NSLog(@"新的文件：%@",newFile);
    //追加扩展名
    newFile = [newFile stringByAppendingPathExtension:@"txt"];
    NSLog(@"追加了扩展名：%@",newFile);
    //删除扩展名
    newFile = [newFile stringByDeletingPathExtension];
    NSLog(@"删除扩展名：%@",newFile);
    
    //拆分路径
    NSArray* pathComponentsArray = [cache pathComponents];
    NSLog(@"Cache目录拆分：%@",pathComponentsArray);
    //获取路径最后一个component
    NSString* lastComponent = [cache lastPathComponent];
    NSLog(@"最后一个component：%@",lastComponent);
}
- (IBAction)pathExist:(id)sender {
    //文件存在性操作
    
    //创建文件夹
    NSString* directoryName = @"myDirectory";
    NSString* cachePath = [MCFileManager cacheDirectory];
    BOOL createResult = [MCFileManager createDirectoryInPath:cachePath directoryName:directoryName];
    if (createResult) {
        NSLog(@"创建文件夹%@成功",directoryName);
    }else{
        NSLog(@"创建文件夹%@失败",directoryName);
    }
    
    //创建文件
    NSString* fileName = @"myFile.txt";
    createResult = [MCFileManager createFileInPath:cachePath fileName:fileName];
    if (createResult) {
        NSLog(@"创建文件%@成功",fileName);
    }else{
        NSLog(@"创建文件%@失败",fileName);
    }
    
    //查询文件是否存在
    BOOL isDirectory;
    NSString* filePath = [cachePath stringByAppendingPathComponent:fileName];
    BOOL fileExist = [MCFileManager fileExistInPath:filePath isDirectory:&isDirectory];
    NSLog(@"文件是否存在：%@  是否是文件夹：%@",fileExist?@"是":@"否",isDirectory?@"是":@"否");
}
- (IBAction)fileSimpleReadWrite:(id)sender {
    
    NSString* fileName = @"myFile.txt";
    NSString* filePath = [[MCFileManager cacheDirectory] stringByAppendingPathComponent:fileName];
    NSString* content = @"Hello World";
    
    
    //写入文件
    if ([MCFileManager writeString:content toPath:filePath]) {
        //写入成功
        NSLog(@"写入文件%@成功",fileName);
    }else{
        //写入失败
        NSLog(@"写入文件%@失败",fileName);
    }
    
    //读取文件
    NSString* readContent = [MCFileManager readFileFromPath:filePath];
    if (readContent.length > 0) {
        //读取到内容
        NSLog(@"读取%@文件内容：%@",fileName,readContent);
    }else{
        NSLog(@"读取不到%@文件的内容",fileName);
    }
}
- (IBAction)fileAdvancedReadWrite:(id)sender {
    
    NSString* fileName = @"myFile.txt";//要写入的文件名称
    NSString* filePath = [[MCFileManager cacheDirectory] stringByAppendingPathComponent:fileName];//文件路径
    NSString* content = @"Hello World";//要写入的内容
    
    //尝试写入文件内容
    if([MCFileManager writeStringUsingFileHandle:content toPath:filePath]){
        //写入文件成功
        NSLog(@"写入%@文件成功",fileName);
    }else{
        NSLog(@"写入%@文件失败",fileName);
    }
    
    //读取文件的内容
    NSString* fileContent = [MCFileManager readFileUsingFileHandleFromPath:filePath];
    NSLog(@"%@文件的内容：%@",fileName, fileContent);
    
    NSString* appendContent = @"你好世界";
    //尝试追加写文件
    if([MCFileManager appendStringUsingFileHandle:appendContent toPath:filePath]){
        //追加写文件成功
        NSLog(@"追加写文件成功");
    }else{
        NSLog(@"追加写文件失败");
    }
    
    //读取文件的内容
    fileContent = [MCFileManager readFileUsingFileHandleFromPath:filePath];
    NSLog(@"%@文件的内容：%@",fileName, fileContent);
    
}
- (IBAction)convertDictAndFile:(id)sender {
    
    NSString* fileName = @"myKeyValue.plist";
    NSString* filePath = [[MCFileManager cacheDirectory] stringByAppendingPathComponent:fileName];
    NSDictionary* dict = @{@"key1":@"value1",@"key2":@"value2"};
    //写入文件
    if ([MCFileManager saveDictionary:dict isPlistFileOfPath:filePath]) {
        //文件写入成功
        NSLog(@"文件%@写入成功",fileName);
    }else{
        //文件写入失败
        NSLog(@"文件%@写入失败",fileName);
    }
    //读取文件
    NSDictionary* dictToRead = [MCFileManager dictionaryInPlistFileOfPath:filePath];
    NSLog(@"读取%@文件内容：%@",fileName,dictToRead);
    
}
- (IBAction)fileBundleFilePath:(id)sender {
    
    NSString* fileName = @"myBundleFile.txt";
    //查找文件路径
    NSString* filePath = [[NSBundle mainBundle]pathForResource:fileName ofType:nil];
    NSLog(@"%@文件的路径:%@",fileName,filePath);
    
}
- (IBAction)printSandBox:(id)sender {
    //递归打印沙盒内文件和文件夹
    [MCFileManager printHierachyOfSandBox];
}

- (IBAction)logToFile:(id)sender {
    
//    [ MCFileManager logWithFormat:@"%@,%@",@"hello",@"你好"];
    PLog(@"%@,%@",@"HELLO",@"你好2");
}
@end







